import addSelButton from "@/components/diy-form/add-sel-button.vue";
import addSelBg from "@/components/diy-form/add-sel-bg.vue";
import selForm from "@/components/diy-form/sel-form.vue";
import diyForm from "@/components/diy-form/diy_form.vue";
import showForm from "@/components/diy-form/show-form.vue";
import MxDatePicker from "@/components/mx-datepicker/mx-datepicker.vue";
import shareWechat from '@/components/share/share-wechat.vue';
import liuyunoTabs from "@/components/liuyuno-tabs/liuyuno-tabs.vue";
import {
	forEach
} from "core-js/library/fn/array";
export default {
	components: {
		addSelBg,
		addSelButton,
		selForm,
		diyForm,
		showForm,
		MxDatePicker,
		shareWechat,
		liuyunoTabs
	},
	data() {
		return {
			currentIndex: 0,
			eventProitlist: [{
				title: '新的要素',
				formData: []
			}],
			s_id: 0,
			temp_id: 0,
			loginStatus: false,
			userInfo: {},
			mobile: '',
			title: '',
			formData: [],
			//日期时间选择数据
			mxDatePicker: {
				showPicker: false,
				type: '',
				start_time: '',
				end_time: '',
				index: -1,
			},
			againMore: -1,
			showPushCircletip: 0,
			pushCircleStatus: -1,
			pushCircleList: [],
			push_circle_index: 0,
			joinRuleList: [],
			joinRuleListIndex: 0,
			circle_id: 0,
			s_config: {},
			loginCallBackType: '',
			// tabs
			defaultConfig: {
				// 要显示的key
				key: 'name',
				// 字体大小 rpx
				fontSize: 26,
				// 字体颜色
				color: '#4d4d4d',
				// 激活字体颜色
				activeColor: '#4d4d4d',
				// item宽度 0为自动
				itemWidth: 0,
				// 下划线左右边距，文字宽度加边距，如果设置了itemWidth则为itemWidth加边距 rpx
				underLinePadding: 10,
				// 下划线宽度 rpx  注意：设置了此值 underLinePadding 失效
				underLineWidth: 0,
				// 下划线高度 rpx
				underLineHeight: 4,
				// 下划线颜色
				underLineColor: '#26c390',
			},
		}
	},
	onLoad(e) {
		// this.joinRuleList = this.$api.joinRuleList();
		//初始化时间
		var initTime = this.$api.getDateRange('', 7, 5);
		this.mxDatePicker.start_time = initTime[0] ? initTime[0] : '';
		this.mxDatePicker.end_time = initTime[1] ? initTime[1] : '';
		// if (e.temp_id) {
		// 	this.temp_id = e.temp_id;
		// 	this.getTempInfo();
		// }
		if (e.s_id) {
			this.s_id = e.s_id;
			this.getEditInfo();
		}

		// if (e.token) {
		this.token =
			'eyJhbGciOiJTSEEyNTYiLCJ0eXAiOiJKV1QifQ==.eyJ0aW1lIjoxNzc2ODQ3MTE0LCJkYXRhIjp7ImFkbWluX2lkIjoxfX0=.yaiWERKtzS8FNa29DsjRHh04QIBgOfx1nThd7JvFEKQ='
		// }

		// if (e.hash_code) {
		this.hash_code = '680dd5c9c9bc81745737161'
		// }



		// 初始化
	},
	onShow() {
		// this.onShowInit();
		//检测是不是登录了
		// if (this.$refs.show_form) {
		// 	this.$refs.show_form.parentSetUsername();
		// }
	},

	methods: {
		// tab切
		tabClick(index) {

			this.currentIndex = index
		},
		//swiper切换
		tabClickswipe(e) {
			this.currentIndex = e.detail.current
		},

		//添加要素
		addevent() {
			let marker = {
				title: '新的要素' + this.eventProitlist.length,
				formData: []
			}
			this.eventProitlist.push(marker)
			// this.currentIndex = this.eventProitlist.length - 1
		},
		wechatRegister(e) {
			if (e.code == 1) {
				var that = this;
				that.onShowInit();
				switch (that.loginCallBackType) {
					case 'pushCircleChange':
						that.pushCircleChange();
						break;
					case 'submit':
						that.submit();
						break;
				}
				that.loginCallBackType = '';
			}
		},
		onShowInit() {
			// this.loginStatus = this.$api.checkLogin(false);
			// this.userInfo = this.$api.checkUserInfo();
			// if (this.temp_id > 0) {
			// 	this.getStatisticsConfig();
			// }
		},
		getStatisticsConfig() {
			var that = this;
			this.$http.post('config/getStatisticsConfig', {
				type: 1,
				temp_id: that.temp_id
			}).then(res => {
				that.s_config = res.data;
			})
		},
		// shareFunc() {
		// 	var userInfo = this.$api.checkUserInfo();
		// 	var title = (this.temp_id > 0 ? '邀请您使用模板发布统计' : '邀请您发布统计');
		// 	var posterData = {
		// 		title: title,
		// 		nickname: userInfo.nickname ? userInfo.nickname : '',
		// 		qrcodeUrl: this.s_config.uni_qrcode,
		// 		path: '/pages/form/statistics/add?temp_id=' + this.temp_id
		// 	};
		// 	this.$refs.share_wechat.setShowStatus(true, posterData);
		// },
		getTempInfo() {
			var that = this;
			that.$api.loadingMsg('正在加载模板数据');
			that.$http.post('Template/getTempInfo', {
				temp_id: that.temp_id
			}).then(res => {
				if (res.code == 1) {
					var info = res.data.info;
					that.title = info.title;
					if (info.bgPath) {
						that.$refs.add_sel_bg.initBgFunc(info.bgPath);
					}
					if (info.join_datalist) {
						that.formData = JSON.parse(JSON.stringify(info.join_datalist))
					}
					if (info.content_list) {
						let join_contentlist = JSON.parse(JSON.stringify(info.content_list));
						that.$refs.add_sel_button.initDataList(join_contentlist)
					}
				}
			})
		},
		againMoreChange(e) {
			var status = e.detail.value;
			this.againMore = (status == true) ? 1 : -1;
		},
		pushCircleChange() {
			var that = this;
			var checkLogin = that.$api.checkLogin(false);
			if (checkLogin == false) {
				that.pushCircleStatus = -1;
				that.loginCallBackType = 'pushCircleChange';
				that.$refs.wechat_login.saveShowStatus(true);
				return;
			}
			var status = (this.pushCircleStatus == -1) ? 1 : -1;
			if (status == 1 && that.showPushCircletip < 1) {
				uni.showModal({
					title: '友情提示',
					content: '只能选择一个圈子，选择圈子发布后不能再次修改!',
					showCancel: false
				})
				that.showPushCircletip++;
			}
			if (status == 1 && this.pushCircleList.length < 1) {
				this.getCircleList();
			}
			this.pushCircleStatus = status;
		},
		getCircleList() {
			var that = this;
			that.$http.post('config/getCreateCircleList').then(res => {
				that.push_circle_index = 0;
				if (res.code == 1) {
					var resDataList = res.data.list;
					that.pushCircleList = resDataList;
					if (that.circle_id > 0) {
						for (var i = 0; i < resDataList.length; i++) {
							if (resDataList[i].circle_id == that.circle_id) {
								that.push_circle_index = i;
								break;
							}
						}
					}
				} else {
					that.pushCircleList = [];
					that.$api.msg(res.msg);
				}
			});
		},
		bindPickerChange(e) {
			this.push_circle_index = e.detail.value;
		},
		joinRuleChange(e) {
			this.joinRuleListIndex = e.detail.value;
			console.log(this.joinRuleListIndex)
		},
		DatePicker(type, index) { //显示
			this.mxDatePicker.index = index;
			this.mxDatePicker.type = type;
			this.mxDatePicker.showPicker = true;
			if (index == 1) {
				var time = this.mxDatePicker.start_time;
			} else {
				var time = this.mxDatePicker.end_time;
			}
			this.mxDatePicker.value = time;
			//this.mxDatePicker.value  = '';
			//this.$forceUpdate();
		},
		DatePickerEnd(e) { //选择
			this.mxDatePicker.showPicker = false;
			if (e) {
				if (this.mxDatePicker.index == 1) {
					this.mxDatePicker.start_time = e.value;
				} else {
					this.mxDatePicker.end_time = e.value;
				}
			}
		},
		previewForm() {
			const that = this;
			var infoParams = {
				// bgPath: {
				// 	path: bgPath,
				// 	qnPath: bgPath
				// },
				start_time: this.mxDatePicker.start_time,
				end_time: this.mxDatePicker.end_time,
				// contentList: this.$refs.add_sel_button.getPreviewContent(),
			};

			let data = []
			// if (infoParams.title == '') {
			// 	this.$api.msg('名称标题不能为空');
			// 	return;
			// }
			this.eventProitlist.forEach((val, index) => {
				var dataList = that.$refs.show_form[index].getItemList();
				if (dataList.length > 0) {
					var opeList = this.$api.opeDiyFormData(dataList);
					if (opeList.length < 1) {
						return;
					}
				} else {
					var opeList = [];
				}
				data.push({
					dataList: dataList,
					contentList: [{
						"type": "textarea",
						"title": "文字",
						"content": val.title
					}]
				});
			})
			infoParams.opeList = data;
			uni.setStorageSync('form-statistics-detail-preview-data', infoParams);


			this.$api.navigateTo('/pages/form/statistics/detail-preview');
			/* uni.setStorageSync('common-preview-cache-data',opeList);
			this.$api.navigateTo('/pages/common/preview-form'); */
		},
		// showSelForm(index) {
		// 	this.currentIndex = index
		// 	this.$refs.sel_form.tggleStatus(true);
		// },
		selFormData: function(info) {
			if (info) {
				const that = this;
				this.$nextTick(function() {
					console.log(info)
					let sdasd = JSON.parse(JSON.stringify(info));
					// that.$refs.show_form.changeItemlist();
					this.eventProitlist[this.currentIndex].formData.push(sdasd);
					if (this.currentIndex === 0) {
						this.eventProitlist[this.currentIndex].title = '新的要素'
					}
				})
			}
		},
		selFormDataChange(data) {
			console.log('数据改变', data);
			const that = this;
			this.$nextTick(function() {
				that.eventProitlist[that.currentIndex].formData = data;
				that.currentEventProit.formData = data;
			})

		},
		inputMobile() {
			var that = this;
			uni.showModal({
				title: '输入手机号',
				content: that.mobile,
				editable: true,
				placeholderText: '',
				success(res) {
					if (res.confirm) {
						var checkMobile = that.$api.isPhoneNumber(res.content);
						if (checkMobile == false) {
							that.mobile = '';
							that.$api.msg('请输入正确的手机号');
						} else {
							that.mobile = res.content;
						}
					}
				}
			})
		},


		// 编辑后的信息
		getEditInfo() {
			var that = this;
			if (that.s_id > 0) {
				that.$http.post('statistics/getEditInfo', {
					s_id: that.s_id,
					token:'eyJhbGciOiJTSEEyNTYiLCJ0eXAiOiJKV1QifQ==.eyJ0aW1lIjoxNzc2ODQ3MTE0LCJkYXRhIjp7ImFkbWluX2lkIjoxfX0=.yaiWERKtzS8FNa29DsjRHh04QIBgOfx1nThd7JvFEKQ='
					// is_copy: (that.isCopy == true) ? 1 : -1
				}).then(res => {
					if (res.code == 1) {
						var info = res.data.info;
						that.info = info;
						let objlist = []
						info.data.forEach(vals => {
							objlist.push({
								formData: vals.join_datalist,
								contentList: vals.content_list
						
							})
							
							// that.$refs.add_sel_button.initDataList(JSON.parse(vals.content_list))
						})
						that.eventProitlist = objlist
		


						that.title = info.title;
						that.againMore = info.again_more;
						if (that.isCopy == false) {
							that.mobile = info.mobile;
							that.mxDatePicker.start_time = info.start_time_text;
							that.mxDatePicker.end_time = info.end_time_text;
							that.pushCircleStatus = info.push_circle_status;
							if (that.pushCircleStatus == 1) {
								that.getCircleList();
							}
							that.userInfo.nickname = info.nickname;
							that.userInfo.avatar = info.avatar;
						} else {
							that.initCopy();

						}
						// that.joinRuleList.forEach((item, index) => {
						// 	if (item.id == info.show_rule_status) {
						// 		that.joinRuleListIndex = index;
						// 	}
						// })

					} else {
						that.$api.msg(res.msg);
					}
				})
			}
		},


		
		async submit() {
			var that = this;

			let postParams = {};

			var text = '',
				url = '';
			if (that.s_id !== 0) {
				text = '确认修改';
				url = 'statistics/edit';
				postParams.s_id = that.s_id;
			} else {
				text = '确认发布';
				url = 'statistics/create';
			}
			that.$api.showModal('text', '', async function(res) {
				//判断需要填写的表单内容
				if (res != 'yes') {
					return false;
				}
				var c_text = '';
				postParams.data = []

				that.eventProitlist.forEach((val, index) => {
					var dataList = that.$refs.show_form[index].getItemList();
					postParams.data.push({
						dataList: dataList,
						contentList: [{
							"type": "textarea",
							"title": "文字",
							"content": val.title
						}]
					});
				})
				postParams.againMore = that.againMore;
				postParams.title = '城市体检模块'
				postParams.mobile = '18196585246'
				postParams.hash_code = that.hash_code
				postParams.token = that.token
				postParams.show_rule_status = 1 //展示谁可见

				that.$api.loadingMsg(text);

				that.$http.post(url, postParams).then(res => {
					that.$api.msg(res.msg);
					if (res.code == 1) {
						setTimeout(function() {
							uni.redirectTo({
								url: '/pages/form/statistics/detail?s_id=' + res
									.data.s_id
							})
							//that.$api.navigateTo('/pages/form/statistics/detail?s_id='+res.data.s_id);
						}, 1000)
					}

				})
			})

		},
		showError(text) {
			if (text) {
				this.$api.msg(text);
			}
			throw 'error';
		}
	}
}